/**
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved. 
 */
package com.wilmar.process.core.assignment;

import java.util.Set;

import org.apache.log4j.Logger;
import org.jbpm.api.model.OpenExecution;
import org.jbpm.api.task.Assignable;
import org.jbpm.api.task.AssignmentHandler;

import com.wilmar.process.core.services.api.IContentService;
import com.wilmar.process.core.services.spi.IAssignmentHandlerFilter;
import com.wilmar.process.core.services.spi.IEmailService;
import com.wilmar.process.entity.Email;
import com.wilmar.process.entity.Organization;
import com.wilmar.process.entity.ProcessParticipant;
import com.wilmar.process.entity.Role;
import com.wilmar.process.entity.User;

/**
 * @author MaKuBEX Email:makubexnet@gmail.com
 * @version 1.0.0 May 26, 2010
 * @since 1.0.0
 * 
 */
public abstract class AbstractAssignmentHandlerImpl implements
		AssignmentHandler {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 5782979304856541038L;

	private static final Logger log = Logger
			.getLogger(AbstractAssignmentHandlerImpl.class);

	protected IAssignmentHandlerFilter filter;
	protected boolean notify = false;
	protected String content;
	protected String subject;

	protected IEmailService emailService;
	protected IContentService contentService;

	/**
	 * 发送邮件
	 * 
	 * @param openExecution
	 *            运行时上下文
	 * @param participant
	 *            参与人(邮件接收人)
	 */
	protected void sendEmail(OpenExecution openExecution,
			ProcessParticipant participant) {
		try {
			if (this.notify) {
				this.subject = this.contentService.resolveContent(this.subject,
						openExecution);
				this.content = this.contentService.resolveContent(this.content,
						openExecution);
				Email email = new Email(this.subject, this.content);
				this.emailService.sendMailToProcessParticipant(email,
						participant);
			}
		} catch (Exception e) {
			log.warn("Could not send notification email");
		}
	}

	/**
	 * 设定参与人
	 * 
	 * @param assignable
	 *            设定接口
	 * @param participant
	 *            参与人集合
	 */
	protected void appendParticipant(Assignable assignable,
			ProcessParticipant participant) {
		if (participant.getOrganizationSet() != null) {
			Set<Organization> c_orgSet = participant.getOrganizationSet();
			for (Organization org : c_orgSet)
				assignable.addCandidateUser(org.getId());
		}
		if (participant.getRoleSet() != null) {
			Set<Role> c_roleSet = participant.getRoleSet();
			for (Role role : c_roleSet)
				assignable.addCandidateUser(role.getId());
		}
		if (participant.getUserSet() != null) {
			Set<User> c_userSet = participant.getUserSet();
			for (User user : c_userSet)
				assignable.addCandidateUser(user.getId());
		}
		if (participant.getAssigne() != null) {
			assignable.setAssignee(participant.getAssigne().getId());
		}
	}

	/**
	 * @return the emailService
	 */
	public IEmailService getEmailService() {
		return emailService;
	}

	/**
	 * @param emailService
	 *            the emailService to set
	 */
	public void setEmailService(IEmailService emailService) {
		this.emailService = emailService;
	}

	/**
	 * @return the contentService
	 */
	public IContentService getContentService() {
		return contentService;
	}

	/**
	 * @param contentService
	 *            the contentService to set
	 */
	public void setContentService(IContentService contentService) {
		this.contentService = contentService;
	}
}
